package com.gleason.gt.adapters;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.gleason.gt.R;
import com.gleason.gt.server.model.database.PlayListEntry;
import com.gleason.gt.widgets.PlaylistButton;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class PlaylistAdapter extends BaseAdapter{
	private Context context;
	private List<PlayListEntry> entries;
	
	public PlaylistAdapter(final Context context, final List<PlayListEntry> playlistEntries){
		this.entries = playlistEntries;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return entries.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return entries.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		PlayListEntry pe = entries.get(position);
		if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
        }
		TextView nextSong = (TextView) convertView.findViewById(R.id.nextsong);
		nextSong.setText(pe.getFile().getFileName());
		PlaylistButton button = (PlaylistButton) convertView.findViewById(R.id.toggle_thumb_up);
		button.setPlaylistId(pe.getId());
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				addThumbsUp((PlaylistButton)v);
				String test="test";
			}
		});
		PlaylistButton td = (PlaylistButton) convertView.findViewById(R.id.toggle_thumb_down);
		button.setPlaylistId(pe.getId());
		td.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				addThumbsDown((PlaylistButton)v);
				String test="test";
			}
		});
		return convertView;
	}
	public void addThumbsUp(PlaylistButton v){
    	HttpClient httpclient = new DefaultHttpClient();
		HttpGet request = new HttpGet(getString(R.string.set_thumbs_up));
		try {
			httpclient.execute(request);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		httpclient.getConnectionManager().shutdown();
    } // end callWebService()
    public void addThumbsDown(PlaylistButton v){
    	HttpClient httpclient = new DefaultHttpClient();
		HttpPost request = new HttpPost(getString(R.string.set_thumbs_down));
		List<NameValuePair> params = new LinkedList<NameValuePair>();
		params.add(new BasicNameValuePair("playlistEntry", v.getPlaylistId().toString()));
		try {
			request.setEntity(new UrlEncodedFormEntity(params));
			httpclient.execute(request);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} 
		httpclient.getConnectionManager().shutdown();
    } // end callWebService()
	

}
